/**
 * jims
 */
package com.jims.master.modules.blood.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.blood.dao.BloodInpDetailDao;
import com.jims.master.modules.blood.dao.BloodInpMasterDao;
import com.jims.master.modules.blood.entity.BloodInpDetail;
import com.jims.master.modules.blood.entity.BloodInpMaster;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 血液入库Service
 * @author fengyg
 * @version 2017-04-07
 */
@Service
@Transactional(readOnly = true)
public class BloodImportService extends CrudService<BloodInpDetailDao, BloodInpDetail> {
    @Autowired
    private BloodInpMasterDao bloodInpMasterDao;

	public BloodInpDetail get(String id) {
		return super.get(id);
	}
	
	public List<BloodInpDetail> findList(BloodInpDetail bloodInpDetail) {
		return super.findList(bloodInpDetail);
	}

    /**
     * 根据入库单号获取入库记录
     * @param page 分页对象
     * @param bloodInpDetail
     * @return
     * @author fengyg
     */
	public Page<BloodInpDetail> findPage(Page<BloodInpDetail> page, BloodInpDetail bloodInpDetail) {
        //List<BloodInpDetail> list = dao.getListByInpNo(bloodInpDetail);
        page.setList(null);
        bloodInpDetail.setPage(page);
		return page;
	}

    /**
     * 获取当前机构最大入库单号
     * @return
     * @author fengyg
     */
    public String getMaxInpNo(){
        return bloodInpMasterDao.getMaxInpNo(UserUtils.getOrgId());
    }

    /**
     * 血液入库保存
     * @param bloodInpDetail
     * @return
     * @author fengyg
     */
	@Transactional(readOnly = false)
	public int saveInp(BloodInpDetail bloodInpDetail) throws Exception{
        int num = 0;
        if(null != bloodInpDetail){
            List<BloodInpDetail> list = bloodInpDetail.getDetailList();
            if(null != list && list.size() >0){
                BloodInpMaster master = list.get(0).getBloodInpMaster();
                master.preInsert();
                master.updateOrgId();
                int n = bloodInpMasterDao.insert(master);  //保存血液入库主表
                if (n <= 0) {
                    num = 0;
                    throw new Exception("主表保存失败!");
                } else {
                    num += n;
                }
                for (BloodInpDetail detail : list) {
                    BloodInpDetail saveDetail = new BloodInpDetail();
                    saveDetail.preInsert();
                    saveDetail.updateOrgId();
                    saveDetail.setInpMasterId(master.getId());
                    saveDetail.setBloodId(detail.getBloodId());
                    saveDetail.setPatBloodGroup(detail.getPatBloodGroupCode());
                    saveDetail.setRh(detail.getRhCode());
                    saveDetail.setComponentId(detail.getComponentId());
                    saveDetail.setBloodCapacity(detail.getBloodCapacity());
                    saveDetail.setInvalDate(detail.getInvalDate());
                    saveDetail.setBloodDonorName(detail.getBloodDonorName());
                    saveDetail.setBloodGatherDate(detail.getBloodGatherDate());
                    saveDetail.setUnits(detail.getUnits());
                    saveDetail.setFlag("0");  //0，未出库
                    int no = dao.insert(saveDetail);   //保存血液入库明细表
                    if(no <= 0){
                        num = 0;
                        throw new Exception("血袋号为" + detail.getBloodId() + "的血袋保存失败!");
                    }else{
                        num += no;
                    }
                }
            }

        }
		return num;
	}
	
	@Transactional(readOnly = false)
	public int delete(BloodInpDetail bloodInpDetail) {
		return super.delete(bloodInpDetail);
	}

    /**
     * 加载血液库存信息
     * @param bloodInpDetail
     * @return
     * @author fengyg
     */
    @Transactional(readOnly = false)
    public List<BloodInpDetail> loadBloodStock(BloodInpDetail bloodInpDetail){
        bloodInpDetail.updateOrgId();
        return dao.loadBloodStock(bloodInpDetail);
    }

    /**
     * 入库单查询
     * @param bloodInpDetail
     * @return
     * @author fengyg
     */
    public List<BloodInpDetail> bloodImportQuery(BloodInpDetail bloodInpDetail){
        bloodInpDetail.updateOrgId();
        return dao.bloodImportQuery(bloodInpDetail);
    }

}